﻿using System;
using Command.Commands;
using Command.Framework;
using CommandLine;
using Utility;

namespace Command
{
	public class Options
	{
		[Option("c","command", Required = true, HelpText = "Name of the command to execute")]
		public string Command;
	}

	class Program
	{
		static void Main(string[] args)
		{
			Locator.Resolver = new StructureMapResolver();

			var options = new Options();
			ICommandLineParser parser = new CommandLineParser(new CommandLineParserSettings(Console.Out));
			if (parser.ParseArguments(args, options))
			{
				if (!string.IsNullOrEmpty(options.Command))
				{
					string commandTypeName = string.Format("{0}.{1}Command", typeof (ICommand).Namespace, options.Command);
					var commandType = Type.GetType(commandTypeName, false, true);
					if (commandType != null)
					{
						var command = Locator.Resolve<ICommand>(commandType);
						if (command != null)
							command.Execute(args);
						else
							Console.WriteLine("Unknown Command: {0}", options.Command);
					}
					else
						Console.WriteLine("Invalid Command: {0}", options.Command);
				}
				else
					Console.WriteLine("Command Missing");
			}
			else
				Console.WriteLine("Error Parsing Arguments");
		}
	}
}